/******************************************************************************
 * @file     flash_test_runtime.c
 * @version  V0.10
 * $Revision: 1 $
 * $Date: 21/02/23 9:23a $
 * @brief    IEC60730 Intrnal Flash Test Using CRC Method
 * @note
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
 ******************************************************************************/

#include "nuc980.h"
#include "IEC60730_CONTROL_PARAM.h"
#include "IEC60730_SAFETY_TEST_LIB.h"

static uint32_t s_u32RomCurrentAddr, s_u32RomEndAddr;
static uint32_t volatile s_u32CHECKSUM = 0;
extern uint32_t checksum_flash(uint8_t *string, uint32_t u32seed, uint32_t u32Length);

void IEC60730_Flash_Test_RunTime_Init(void)
{
    s_u32RomCurrentAddr = (uint32_t)ROM_START;
    s_u32RomEndAddr = s_u32RomCurrentAddr + ROM_LENGTH;
	
	s_u32CHECKSUM = CHECKSUM_SEED;
}

uint8_t IEC60730_Flash_Test_RunTime(void)
{
    uint32_t u32StartAddr;
    uint8_t u8TestResult;

    u32StartAddr = s_u32RomCurrentAddr;

    s_u32CHECKSUM = checksum_flash((uint8_t*)u32StartAddr, s_u32CHECKSUM, (uint32_t)ROM_RUNTIME_TEST_LENGTH);

    /* check if end address */		
    u32StartAddr += (uint32_t)ROM_RUNTIME_TEST_LENGTH;
    if (u32StartAddr >= s_u32RomEndAddr) {
        s_u32RomCurrentAddr = (uint32_t)ROM_START;
#if 1
	printf("s_u32CHECKSUM = 0x%x !!! \n", s_u32CHECKSUM);
#endif
        if(s_u32CHECKSUM != __Check_Sum)
            u8TestResult = TEST_FAIL;
        else
            u8TestResult = TEST_PASS;
		
#if 1
	s_u32CHECKSUM = CHECKSUM_SEED;
#else
	printf("test result = 0x%x !!!\n", u8TestResult) ;
		
	while(1);	
#endif
    }
    else {
        s_u32RomCurrentAddr = u32StartAddr;
        u8TestResult = TEST_PASS;
    }

    return u8TestResult;
}

/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
